let db = require('./01-封装函数db')
const express = require('express')
const app = express()
const utils = require('utility');    //es6语法  let { md5 } = require('utility')
//接收post请求体
app.use(express.urlencoded({ extended: true }));
app.post('/api/reguser', function (req, res) {
    let { username, password } = req.body
    // console.log(req.body);
    db.db(`select * from user where username='${username}'`, (e, r) => {
        if (e) throw e
        // console.log(r);
        if (r.length > 0) {
            res.send({ status: 1, message: '用户名已存在' })
        } else {
            //注册前加密密码
            password = utils.md5(password)  //es6语法  password = md5(password)
            db.db(`insert into user set username="${username}" ,password="${password}"`, (err, result) => {
                if (err) throw err
                res.send({ status: 0, message: '注册成功' })
            })
        }
    })
})

app.listen(3000, function () {
    console.log('服务器启动了')
})